#include "HttpServer.hpp"
using namespace HttpServerModule;
#include "HttpProtocol.hpp"

std::string Login(Request&req,Response&res)
{
    std::string args;
    if (req.Method() == "GET"){
        std::string url = req.GetUrl();
        args = url.substr(url.find("?")+1);
    }else 
    {
        args = req.GetBody();
    }
    //解析参数
    auto pos1 = args.find("=");
    auto pos2 = args.find("&");
    //usrname=1234&passwd=123
    std::string usrname =  args.substr(pos1+1,pos2-pos1-1);
    auto pos3 = args.find("=",pos2);
    std::string passwd = args.substr(pos3+1); 
    if (usrname == "nihao" && passwd == "123456")
    {
        req.SetUrl(successpage);
    }
    else 
    {
        req.SetUrl(errorspage);
    }
    return res.Build(req);
}

int main(int argc, char* argv[])
{
    if (argc != 2){
        LOG(LogLevel::ERROR) << "Please use ./httserver + port!";
        return 1;
    }
    uint16_t port = std::stoi(argv[1]);
    std::shared_ptr<HttpServer> _httsvr = std::make_shared<HttpServer>(port);
    _httsvr->RegisterHandler("/login",Login);
    _httsvr->Start();
    return 0;
}
